package it.blueocean.acanto.taglibrary.jsp.taglib.tags.dataset;

import it.blueocean.acanto.taglibrary.jsp.taglib.tags.AbstractComponentStateBean;
import it.blueocean.acanto.taglibrary.jsp.taglib.tags.AbstractUIComponentTag;
import it.blueocean.acanto.taglibrary.jsp.taglib.tags.exceptions.ComponentStateBeanNotFoundException;

import javax.servlet.jsp.JspException;

import org.apache.taglibs.standard.lang.support.ExpressionEvaluatorManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class DataSetTag extends AbstractUIComponentTag
{
	private static final long serialVersionUID = -5929683850409792453L;

	private final static Logger logger_ = LoggerFactory.getLogger(DataSetTag.class);

	private String queryExpr_ = null;

	private String pageNumberExpr_ = null;

	private String resultsPerPageExpr_ = null;

	private String orderByExpr_;

	private String filterExpr_;

	private String query_;

	private int pageNumber_;

	private int resultsPerPage_;

	private String orderBy_;

	private String filter_;

	public AbstractComponentStateBean initializeState() throws JspException
	{
		try
		{
			Object obj = DatasetFactory.getInstance().buildDataset();
			((DataSetStateBean) (obj)).setCurrentPage(pageNumber_);
			((DataSetStateBean) (obj)).setResultsPerPage(resultsPerPage_);
			((DataSetStateBean) (obj)).setOrderBy(orderBy_);
			((DataSetStateBean) (obj)).setFilter(filter_);
			return ((AbstractComponentStateBean) (obj));
		}
		catch (Exception exception)
		{
			logger_.error("Exception instantiating DataSetTag", exception);
			throw new JspException(exception);
		}
	}

	public String getQuery()
	{
		return this.query_;
	}

	public void setPageNumber(String paramString)
	{
		this.pageNumberExpr_ = paramString;
	}

	public void setQuery(String paramString)
	{
		this.queryExpr_ = paramString;
	}

	public void setResultsPerPage(String paramString)
	{
		this.resultsPerPageExpr_ = paramString;
	}

	public void setOrderBy(String paramString)
	{
		this.orderByExpr_ = paramString;
	}

	public void setFilter(String paramString)
	{
		this.filterExpr_ = paramString;
	}

	public void setParam(String paramString, Object paramObject)
	{
		try
		{
			DataSetStateBean localDataSetStateBean = (DataSetStateBean) getStateBean();
			localDataSetStateBean.setParam(paramObject);
		}
		catch (ComponentStateBeanNotFoundException localComponentStateBeanNotFoundException)
		{
			logger_.error(localComponentStateBeanNotFoundException.getMessage());
		}
	}

	public int doStartTag(AbstractComponentStateBean abstractcomponentstatebean) throws JspException
	{
		DataSetStateBean datasetstatebean = (DataSetStateBean) abstractcomponentstatebean;
		datasetstatebean.setQuery(query_);

		datasetstatebean.getParams().clear();
		
		return 2;
	}

	public int doEndTag(AbstractComponentStateBean paramAbstractComponentStateBean) throws JspException
	{
		DataSetStateBean localDataSetStateBean = (DataSetStateBean) paramAbstractComponentStateBean;
		this.pageContext.setAttribute(getId(), new PageCachedDataSet(localDataSetStateBean));
		return 6;
	}

	protected void evaluateExpressions() throws Exception
	{
		super.evaluateExpressions();
		this.query_ = ((String) ExpressionEvaluatorManager.evaluate("sql", this.queryExpr_, String.class, this,
				this.pageContext));
		if(this.pageNumberExpr_ != null)
			this.pageNumber_ = ((Integer) ExpressionEvaluatorManager.evaluate("pageNumber", this.pageNumberExpr_,
					Integer.class, this, this.pageContext)).intValue();
		else
			this.pageNumber_ = 1;
		if(this.resultsPerPageExpr_ == null)
			this.resultsPerPage_ = -1;
		else
			this.resultsPerPage_ = ((Integer) ExpressionEvaluatorManager.evaluate("resultsPerPage",
					this.resultsPerPageExpr_, Integer.class, this, this.pageContext)).intValue();
		
		if(this.orderByExpr_ != null)
			this.orderBy_ = ((String) ExpressionEvaluatorManager.evaluate("orderBy", this.orderByExpr_, String.class,
					this, this.pageContext));
		else
			this.orderBy_ = null;
		if(this.filterExpr_ != null)
			this.filter_ = ((String) ExpressionEvaluatorManager.evaluate("filter", this.filterExpr_, String.class,
					this, this.pageContext));
		else
			this.filter_ = null;
	}

}
